package com.chengcang.admin.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengcang.admin.common.annotation.LogOperation;
import com.chengcang.admin.common.utils.ExcelUtils;
import com.chengcang.admin.modules.sys.dto.SysDictDataDTO;
import com.chengcang.admin.modules.sys.dto.SysProductDTO;
import com.chengcang.admin.modules.sys.excel.SysProductExcel;
import com.chengcang.admin.modules.sys.service.SysProductService;
import com.chengcang.common.constant.Constant;
import com.chengcang.common.exception.CustomException;
import com.chengcang.common.utils.Result;
import com.chengcang.common.validator.AssertUtils;
import com.chengcang.common.validator.ValidatorUtils;
import com.chengcang.common.validator.group.AddGroup;
import com.chengcang.common.validator.group.DefaultGroup;
import com.chengcang.common.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-01-10
 */
@RestController
@RequestMapping("sys/product")
@Api(tags="产品")
public class SysProductController {
    @Autowired
    private SysProductService sysProductService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "condition", value = "命名和编号", paramType = "query", dataType="String")
    })
    //@RequiresPermissions("sys:product:page")
    public Result<Page<SysProductDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<Page<SysProductDTO>>().ok(sysProductService.getPage(params));
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    //@RequiresPermissions("sys:product:info")
    public Result<SysProductDTO> get(@PathVariable("id") Long id){
        SysProductDTO detail = sysProductService.getDetail(id);
        return new Result<SysProductDTO>().ok(detail);
    }

    @GetMapping("spec/{id}")
    @ApiOperation("获取规格")
    public Result<List<SysDictDataDTO>> getSpec(@PathVariable("id") Long id){
        return new Result<List<SysDictDataDTO>>().ok(sysProductService.getSpec(id));
    }


    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    //@RequiresPermissions("sys:product:save")
    public Result save(@RequestBody @Validated SysProductDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        sysProductService.insert(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    //@RequiresPermissions("sys:product:update")
    public Result update(@RequestBody SysProductDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        sysProductService.update(dto);
        return new Result();
    }

    @PutMapping("/batch")
    @ApiOperation("批量修改")
    @LogOperation("批量修改")
    //@RequiresPermissions("sys:product:updateBatch")
    public Result batchUpdate(@RequestBody List<SysProductDTO> dto){
        if(dto == null || dto.size() == 0) return new Result();
        if(dto.get(0).getProductTypeId() == null) throw new CustomException("请选择类型!");
        if(dto.get(0).getStatus() == null) throw new CustomException("请选择状态");
        sysProductService.batchUpdate(dto);
        return new Result();
    }


    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    //@RequiresPermissions("sys:product:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        sysProductService.delete(ids);
        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    //@RequiresPermissions("sys:product:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SysProductDTO> list = sysProductService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, SysProductExcel.class);
    }

}
